<?php
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\ItemsModel;
use app\admin\model\SiteModel;

class SiteController extends AdminBaseController
{
    
    /**
     * 工位列表
     * @adminMenu(
     *     'name'   => '工位',
     *     'parent' => 'admin/Site/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10,
     *     'icon'   => '',
     *     'remark' => '工位管理',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $list = ItemsModel::all(function($query){
            $query->where(['one_price'=>['>',50]])->order('id asc');
        },'site');
        $this->assign('list',$list);
        
        return $this->fetch();
    }
    
    /**
     * 工位添加
     * @adminMenu(
     *     'name'   => '工位添加',
     *     'parent' => 'admin/Site/index',
     *     'display'=> true,
     *     'hasView'=> false,
     *     'order'  => 10,
     *     'icon'   => '',
     *     'remark' => '工位添加',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        if(request()->isPost()){
            $request = $this->request->param();
            
            $items = new ItemsModel();
            $items->site->name = $request['name'];
            $items->site->img = $request['img'];
            $items->site->address = $request['address'];
            $items->together('site')->save();
        }
        
        $data = [];
        $this->assign('data',$data);
        
        return $this->fetch();
    }
    
    /**
     * 工位修改
     * @adminMenu(
     *     'name'   => '工位修改',
     *     'parent' => 'admin/Site/index',
     *     'display'=> true,
     *     'hasView'=> false,
     *     'order'  => 10,
     *     'icon'   => '',
     *     'remark' => '工位修改',
     *     'param'  => ''
     * )
     */
    public function edit($id = null)
    {
        if(request()->isPost()){
            $request = $this->request->param();
            $items = ItemsModel::get($id);
            $items->site->name = $request['name'];
            $items->site->img = $request['img'];
            $items->site->detail_pics = $request['detail_pics'];
            $items->site->top_pics = $request['top_pics'];
            $items->site->address = $request['address'];
            $re = $items->together('site')->save();
            if($re){
                $this->success('修改成功');
            }
            else{
                $this->error('修改失败');
            }
        }
        else{
            $data = ItemsModel::get($id,'site');
            $this->assign('data',$data);
            
            return $this->fetch();
        }
    }
}